home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Graph.java < prev    next >
Text File  |  1998-09-15  |  11KB  |  421 lines

  1. /*
  2.  * @(#)Graph.java    1.5 97/07/31
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.util.*;
  32. import java.awt.*;
  33. import java.applet.Applet;
  34. import java.awt.event.*;
  35.  
  36.  
  37. class Node {
  38.     double x;
  39.     double y;
  40.  
  41.     double dx;
  42.     double dy;
  43.  
  44.     boolean fixed;
  45.  
  46.     String lbl;
  47. }
  48.  
  49.  
  50. class Edge {
  51.     int from;
  52.     int to;
  53.  
  54.     double len;
  55. }
  56.  
  57.  
  58. class GraphPanel extends Panel implements Runnable, MouseListener {
  59.     Graph graph;
  60.     int nnodes;
  61.     Node nodes[] = new Node[100];
  62.  
  63.     int nedges;
  64.     Edge edges[] = new Edge[200];
  65.  
  66.     Thread relaxer;
  67.     boolean stress;
  68.     boolean random;
  69.  
  70.     GraphPanel(Graph graph) {
  71.     this.graph = graph;
  72.     addMouseListener(this);
  73.     }
  74.  
  75.     int findNode(String lbl) {
  76.     for (int i = 0 ; i < nnodes ; i++) {
  77.         if (nodes[i].lbl.equals(lbl)) {
  78.         return i;
  79.         }
  80.     }
  81.     return addNode(lbl);
  82.     }
  83.     int addNode(String lbl) {
  84.     Node n = new Node();
  85.     n.x = 10 + 380*Math.random();
  86.     n.y = 10 + 380*Math.random();
  87.     n.lbl = lbl;
  88.     nodes[nnodes] = n;
  89.     return nnodes++;
  90.     }
  91.     void addEdge(String from, String to, int len) {
  92.     Edge e = new Edge();
  93.     e.from = findNode(from);
  94.     e.to = findNode(to);
  95.     e.len = len;
  96.     edges[nedges++] = e;
  97.     }
  98.  
  99.     public void run() {
  100.         Thread me = Thread.currentThread();
  101.     while (relaxer == me) {
  102.         relax();
  103.         if (random && (Math.random() < 0.03)) {
  104.         Node n = nodes[(int)(Math.random() * nnodes)];
  105.         if (!n.fixed) {
  106.             n.x += 100*Math.random() - 50;
  107.             n.y += 100*Math.random() - 50;
  108.         }
  109.         graph.play(graph.getCodeBase(), "audio/drip.au");
  110.         }
  111.         try {
  112.         Thread.sleep(100);
  113.         } catch (InterruptedException e) {
  114.         break;
  115.         }
  116.     }
  117.     }
  118.  
  119.     synchronized void relax() {
  120.     for (int i = 0 ; i < nedges ; i++) {
  121.         Edge e = edges[i];
  122.         double vx = nodes[e.to].x - nodes[e.from].x;
  123.         double vy = nodes[e.to].y - nodes[e.from].y;
  124.         double len = Math.sqrt(vx * vx + vy * vy);
  125.         double f = (edges[i].len - len) / (len * 3) ;
  126.         double dx = f * vx;
  127.         double dy = f * vy;
  128.  
  129.         nodes[e.to].dx += dx;
  130.         nodes[e.to].dy += dy;
  131.         nodes[e.from].dx += -dx;
  132.         nodes[e.from].dy += -dy;
  133.     }
  134.  
  135.     for (int i = 0 ; i < nnodes ; i++) {
  136.         Node n1 = nodes[i];
  137.         double dx = 0;
  138.         double dy = 0;
  139.  
  140.         for (int j = 0 ; j < nnodes ; j++) {
  141.         if (i == j) {
  142.             continue;
  143.         }
  144.         Node n2 = nodes[j];
  145.         double vx = n1.x - n2.x;
  146.         double vy = n1.y - n2.y;
  147.         double len = vx * vx + vy * vy;
  148.         if (len == 0) {
  149.             dx += Math.random();
  150.             dy += Math.random();
  151.         } else if (len < 100*100) {
  152.             dx += vx / len;
  153.             dy += vy / len;
  154.         }
  155.         }
  156.         double dlen = dx * dx + dy * dy;
  157.         if (dlen > 0) {
  158.         dlen = Math.sqrt(dlen) / 2;
  159.         n1.dx += dx / dlen;
  160.         n1.dy += dy / dlen;
  161.         }
  162.     }
  163.  
  164.     Dimension d = getSize();
  165.     for (int i = 0 ; i < nnodes ; i++) {
  166.         Node n = nodes[i];
  167.         if (!n.fixed) {
  168.         n.x += Math.max(-5, Math.min(5, n.dx));
  169.         n.y += Math.max(-5, Math.min(5, n.dy));
  170.         //System.out.println("v= " + n.dx + "," + n.dy);
  171.         if (n.x < 0) {
  172.             n.x = 0;
  173.         } else if (n.x > d.width) {
  174.             n.x = d.width;
  175.         }
  176.         if (n.y < 0) {
  177.             n.y = 0;
  178.         } else if (n.y > d.height) {
  179.             n.y = d.height;
  180.         }
  181.         }
  182.         n.dx /= 2;
  183.         n.dy /= 2;
  184.     }
  185.     repaint();
  186.     }
  187.  
  188.     Node pick;
  189.     boolean pickfixed;
  190.     Image offscreen;
  191.     Dimension offscreensize;
  192.     Graphics offgraphics;
  193.  
  194.     final Color fixedColor = Color.red;
  195.     final Color selectColor = Color.pink;
  196.     final Color edgeColor = Color.black;
  197.     final Color nodeColor = new Color(250, 220, 100);
  198.     final Color stressColor = Color.darkGray;
  199.     final Color arcColor1 = Color.black;
  200.     final Color arcColor2 = Color.pink;
  201.     final Color arcColor3 = Color.red;
  202.  
  203.     public void paintNode(Graphics g, Node n, FontMetrics fm) {
  204.     int x = (int)n.x;
  205.     int y = (int)n.y;
  206.     g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor));
  207.     int w = fm.stringWidth(n.lbl) + 10;
  208.     int h = fm.getHeight() + 4;
  209.     g.fillRect(x - w/2, y - h / 2, w, h);
  210.     g.setColor(Color.black);
  211.     g.drawRect(x - w/2, y - h / 2, w-1, h-1);
  212.     g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent());
  213.     }
  214.  
  215.     public synchronized void update(Graphics g) {
  216.     Dimension d = getSize();
  217.     if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
  218.         offscreen = createImage(d.width, d.height);
  219.         offscreensize = d;
  220.         offgraphics = offscreen.getGraphics();
  221.         offgraphics.setFont(getFont());
  222.     }
  223.  
  224.     offgraphics.setColor(getBackground());
  225.     offgraphics.fillRect(0, 0, d.width, d.height);
  226.     for (int i = 0 ; i < nedges ; i++) {
  227.         Edge e = edges[i];
  228.         int x1 = (int)nodes[e.from].x;
  229.         int y1 = (int)nodes[e.from].y;
  230.         int x2 = (int)nodes[e.to].x;
  231.         int y2 = (int)nodes[e.to].y;
  232.         int len = (int)Math.abs(Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) - e.len);
  233.         offgraphics.setColor((len < 10) ? arcColor1 : (len < 20 ? arcColor2 : arcColor3)) ;
  234.         offgraphics.drawLine(x1, y1, x2, y2);
  235.         if (stress) {
  236.         String lbl = String.valueOf(len);
  237.         offgraphics.setColor(stressColor);
  238.         offgraphics.drawString(lbl, x1 + (x2-x1)/2, y1 + (y2-y1)/2);
  239.         offgraphics.setColor(edgeColor);
  240.         }
  241.     }
  242.  
  243.     FontMetrics fm = offgraphics.getFontMetrics();
  244.     for (int i = 0 ; i < nnodes ; i++) {
  245.         paintNode(offgraphics, nodes[i], fm);
  246.     }
  247.     g.drawImage(offscreen, 0, 0, null);
  248.     }
  249.  
  250.     //1.1 event handling
  251.     public void mouseClicked(MouseEvent e) {
  252.     }
  253.  
  254.     public void mousePressed(MouseEvent e) {
  255.     double bestdist = Double.MAX_VALUE;
  256.     int x = e.getX();
  257.     int y = e.getY();
  258.     for (int i = 0 ; i < nnodes ; i++) {
  259.         Node n = nodes[i];
  260.         double dist = (n.x - x) * (n.x - x) + (n.y - y) * (n.y - y);
  261.         if (dist < bestdist) {
  262.         pick = n;
  263.         bestdist = dist;
  264.         }
  265.     }
  266.     pickfixed = pick.fixed;
  267.     pick.fixed = true;
  268.     pick.x = x;
  269.     pick.y = y;
  270.     repaint();
  271.     e.consume();
  272.     }
  273.  
  274.     public void mouseReleased(MouseEvent e) {
  275.     pick.x = e.getX();
  276.     pick.y = e.getY();
  277.     pick.fixed = pickfixed;
  278.     pick = null;
  279.     repaint();
  280.     e.consume();
  281.     }
  282.  
  283.     public void mouseEntered(MouseEvent e) {
  284.     }
  285.  
  286.     public void mouseExited(MouseEvent e) {
  287.     }
  288.  
  289.     public void mouseDragged(MouseEvent e) {
  290.     pick.x = e.getX();
  291.     pick.y = e.getY();
  292.     repaint();
  293.     e.consume();
  294.     }
  295.  
  296.     public void mouseMoved(MouseEvent e) {
  297.     }
  298.  
  299.     public void start() {
  300.     relaxer = new Thread(this);
  301.     relaxer.start();
  302.     }
  303.  
  304.     public void stop() {
  305.     relaxer = null;
  306.     }
  307.  
  308. }
  309.  
  310.  
  311. public class Graph extends Applet implements ActionListener, ItemListener {
  312.  
  313.     GraphPanel panel;
  314.     Panel controlPanel;
  315.  
  316.     Button scramble = new Button("Scramble");
  317.     Button shake = new Button("Shake");
  318.     Checkbox stress = new Checkbox("Stress");
  319.     Checkbox random = new Checkbox("Random");
  320.  
  321.     public void init() {
  322.     setLayout(new BorderLayout());
  323.  
  324.     panel = new GraphPanel(this);
  325.     add("Center", panel);
  326.     controlPanel = new Panel();
  327.     add("South", controlPanel);
  328.  
  329.     controlPanel.add(scramble); scramble.addActionListener(this);
  330.     controlPanel.add(shake); shake.addActionListener(this);
  331.     controlPanel.add(stress); stress.addItemListener(this);
  332.     controlPanel.add(random); random.addItemListener(this);
  333.  
  334.     String edges = getParameter("edges");
  335.     for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) {
  336.         String str = t.nextToken();
  337.         int i = str.indexOf('-');
  338.         if (i > 0) {
  339.         int len = 50;
  340.         int j = str.indexOf('/');
  341.         if (j > 0) {
  342.             len = Integer.valueOf(str.substring(j+1)).intValue();
  343.             str = str.substring(0, j);
  344.         }
  345.         panel.addEdge(str.substring(0,i), str.substring(i+1), len);
  346.         }
  347.     }
  348.     Dimension d = getSize();
  349.     String center = getParameter("center");
  350.     if (center != null){
  351.         Node n = panel.nodes[panel.findNode(center)];
  352.         n.x = d.width / 2;
  353.         n.y = d.height / 2;
  354.         n.fixed = true;
  355.     }
  356.     }
  357.  
  358.     public void destroy() {
  359.         remove(panel);
  360.         remove(controlPanel);
  361.     }
  362.  
  363.     public void start() {
  364.     panel.start();
  365.     }
  366.  
  367.     public void stop() {
  368.     panel.stop();
  369.     }
  370.  
  371.     public void actionPerformed(ActionEvent e) {
  372.     Object src = e.getSource();
  373.  
  374.     if (src == scramble) {
  375.         play(getCodeBase(), "audio/computer.au");
  376.         Dimension d = getSize();
  377.         for (int i = 0 ; i < panel.nnodes ; i++) {
  378.         Node n = panel.nodes[i];
  379.         if (!n.fixed) {
  380.             n.x = 10 + (d.width-20)*Math.random();
  381.             n.y = 10 + (d.height-20)*Math.random();
  382.         }
  383.         }
  384.         return;
  385.     }
  386.  
  387.     if (src == shake) {
  388.         play(getCodeBase(), "audio/gong.au");
  389.         Dimension d = getSize();
  390.         for (int i = 0 ; i < panel.nnodes ; i++) {
  391.         Node n = panel.nodes[i];
  392.         if (!n.fixed) {
  393.             n.x += 80*Math.random() - 40;
  394.             n.y += 80*Math.random() - 40;
  395.         }
  396.         }
  397.     }
  398.  
  399.     }
  400.  
  401.     public void itemStateChanged(ItemEvent e) {
  402.     Object src = e.getSource();
  403.     boolean on = e.getStateChange() == ItemEvent.SELECTED;
  404.     if (src == stress) panel.stress = on;
  405.     else if (src == random) panel.random = on;
  406.     }
  407.  
  408.     public String getAppletInfo() {
  409.     return "Title: GraphLayout \nAuthor: <unknown>";
  410.     }
  411.  
  412.     public String[][] getParameterInfo() {
  413.     String[][] info = {
  414.         {"edges", "delimited string", "A comma-delimited list of all the edges.  It takes the form of 'C-N1,C-N2,C-N3,C-NX,N1-N2/M12,N2-N3/M23,N3-NX/M3X,...' where C is the name of center node (see 'center' parameter) and NX is a node attached to the center node.  For the edges connecting nodes to eachother (and not to the center node) you may (optionally) specify a length MXY separated from the edge name by a forward slash."},
  415.         {"center", "string", "The name of the center node."}
  416.     };
  417.     return info;
  418.     }
  419.  
  420. }
  421.